package com.itic.mall.system.goods.goodscategory.service;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.service.CrudService;
import com.itic.mall.system.inter.goods.goodscategory.persistence.dao.GoodsCategoryDao;
import com.itic.mall.system.inter.goods.goodscategory.persistence.model.GoodsCategory;
import com.itic.mall.system.inter.goods.goodscategory.persistence.model.GoodsCategoryExample;
import com.itic.mall.system.inter.goods.goodscategory.persistence.model.GoodsCategoryExample.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class GoodsCategoryService
     extends CrudService<GoodsCategoryDao,GoodsCategory,GoodsCategoryExample> {
    @Autowired
    private GoodsCategoryDao goodsCategoryDao;
    /**
     * 根据目录id获取目录名称
     * @param id
     * @return
     */
    public String getCtegoryNameById(String id){
    	String name = null;
    	GoodsCategoryExample caexample = new GoodsCategoryExample();
		com.itic.mall.system.inter.goods.goodscategory.persistence.model.GoodsCategoryExample.Criteria criteriaca = caexample.createCriteria();
		criteriaca.andIsEffectiveEqualTo(DBConst.TRUE);
		criteriaca.andIdEqualTo(id);
		List<GoodsCategory> list = this.goodsCategoryDao.selectByExample(caexample);
		if(list.size()>0){
			name = list.get(0).getName();
		}
		return name;
    }
    /**
     * 获取所有一级分类
     * @return
     */
    public List<GoodsCategory> getOneLevelGoodsCategoryList(){
    	GoodsCategoryExample caexample = new GoodsCategoryExample();
		com.itic.mall.system.inter.goods.goodscategory.persistence.model.GoodsCategoryExample.Criteria criteriaca = caexample.createCriteria();
		criteriaca.andIsEffectiveEqualTo(DBConst.TRUE);
		criteriaca.andPidEqualTo("000");
		List<GoodsCategory> list = this.goodsCategoryDao.selectByExample(caexample);
		return list;
    }
    /**
     * 根据父类id查询最大的orgCade  
     * @author 王文鑫
     * @date 2018年1月5日 下午3:41:25
     * @param pid
     * @return
     */
    public String getMaxCodeByPid(String pId1) {
		return goodsCategoryDao.getMaxCodeByPid(pId1);
	}
    
    /**
     * 根据categoryId 获取 CodeId
     * @author 王文鑫
     * @date 2018年1月19日 下午3:18:02
     * @param categoryId
     * @return
     */
    public String getCodeIdByCategoryId(String categoryId) {
		return goodsCategoryDao.getCodeIdByCategoryId(categoryId);
	}
    
    /**
     * 根据codeId获取商品分类
     * @author 王文鑫
     * @date 2018年1月21日 下午1:31:11
     * @param categoryId
     * @return
     */
    public GoodsCategory getGoodsCategoryByCodeId(String categoryId){
    	return goodsCategoryDao.getGoodsCategoryByCodeId(categoryId);
    }
    
    
    /**
     * 根据父类codeId获取所有子分类
     * @author 王文鑫
     * @date 2018年1月15日 上午11:38:23
     * @param pId
     * @return
     */
    public List<GoodsCategory> getCategoryBycodeId(String codeId) {
		return goodsCategoryDao.getCategoryBycodeId(codeId);
	}
    
    /**
     * 根据商品分类id查询商品总数
     * @author 王文鑫
     * @date 2018年1月7日 下午3:57:41
     * @param categoryId
     * @return
     */
    public int getGoodsNumByCategoryId(String codeid){
    	return goodsCategoryDao.getGoodsNumByCategoryId(codeid);
    }
    
    /**
     * 级联删除
     * 
     * @param id
     * @return
     */
    public String deleteTree(String id) {
        int code = -1;
        GoodsCategory goodsCategory = new GoodsCategory();
        goodsCategory.setId(id);
        goodsCategory.setIsEffective(DBConst.TRUE);
        
		//先删除本级的ID
        GoodsCategory one = goodsCategoryDao.selectByPrimaryKey(id);
		
		if( null != one ){
			code += goodsCategoryDao.deleteByPrimaryKey(id);
			//删除下级
			GoodsCategoryExample rExample = new GoodsCategoryExample();// 逻辑删除资源信息
			Criteria criteria = rExample.createCriteria();
			criteria.andIsEffectiveEqualTo(DBConst.TRUE);
			criteria.andPidEqualTo(one.getCodeid());
			code += goodsCategoryDao.deleteByExample(rExample);
		}
        
        return 0 <= code ? SUCCESS : FAILED;
    }
}